﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PS3Helpers.UserControls
{
    public partial class InputBox : Form
    {
        // Return null for text accepted.
        // Return an empty string if message has been handled by the validation method.
        // Return the error text if the InputBox should show an error message with the returned text.
        public delegate string InputBoxValidationDelegate(InputBox sender, string currenttext);

        private InputBoxValidationDelegate _validationDelegate;

        private InputBox()
        {
            InitializeComponent();
        }

        public static DialogResult Show(string title, string prompt, ref string returnvalue)
        {
            return Show(title, prompt, ref returnvalue, null);
        }

        public static DialogResult Show(string title, string prompt, ref string returnvalue, InputBoxValidationDelegate validationdelegate)
        {
            InputBox inputbox = new InputBox();
            inputbox.Text = title;
            inputbox.labelPromptText.Text = prompt;
            inputbox.textBox.Text = returnvalue;
            inputbox._validationDelegate = validationdelegate;

            DialogResult result = inputbox.ShowDialog();
            returnvalue = inputbox.textBox.Text;
            return result;
        }

        private void buttonOk_Click(object sender, EventArgs e)
        {
            if (_validationDelegate != null)
            {
                string validationstr = _validationDelegate(this, textBox.Text);
                if (validationstr != null)
                {
                    if (validationstr != "")
                    {
                        MessageBox.Show(validationstr, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }

                    return;
                }
            }

            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
